function otype = ostype(tag)
% ostype  - returns information about the currently used OS
%
% FORMAT:       otype = ostype([tag])
%
% Input fields:
%
%       tag         optionally requesting a subfield of the struct
%
% Output fields:
%
%       otype       1x1 struct with fields
%        .computer  copy of computer()
%        .filesep   copy of filesep()
%        .ispc      copy of ispc()
%        .machine   either 'MAC', 'UNIX', or 'WIN'
%
% See also COMPUTER, FILESEP, ISPC

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% declare and (if needed) init persitently stored variable
persistent bvqx_osinfo;
if isempty(bvqx_osinfo) || ...
   ~isstruct(bvqx_osinfo) || ...
   ~bvqx_osinfo.is_initialized

    % initialize persistent variable
    bvqx_osinfo = struct;
    bvqx_osinfo.is_initialized = false;

    % get values
    bvqx_osinfo.computer = upper(computer);
    bvqx_osinfo.filesep = filesep;
    bvqx_osinfo.ispc = ispc;

    if ~isempty(strfind(bvqx_osinfo.computer, 'WIN'))
        bvqx_osinfo.machine = 'WIN';
    elseif ~isempty(strfind(bvqx_osinfo.computer, 'MAC'))
        bvqx_osinfo.machine = 'MAC';
    else
        bvqx_osinfo.machine = 'UNIX';
    end

    % sanity checks
    if (strcmp(bvqx_osinfo.machine, 'UNIX') && filesep ~= '/') || ...
       (strcmp(bvqx_osinfo.machine, 'WIN')  && filesep ~= '\')
        error( ...
            'BVQXtools:InternalError', ...
            'Initialization failed. Bad filesep for machine type %s', ...
            bvqx_osinfo.machine ...
        );
    end

    % set initialized to true
    bvqx_osinfo.is_initialized = true;
end

% argument check
if nargin < 1 || ...
   ~ischar(tag) || ...
    isempty(tag)
    tag = 'full';
else
    tag = lower(tag(:));
end

switch (tag)
    case {'filesep'}
        otype = bvqx_osinfo.filesep;
    case {'machine'}
        otype = bvqx_osinfo.machine;
    otherwise
        otype = bvqx_osinfo;
end
